<?php

class TrxReceiptController extends Controller
{
	
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}
	
	public function actionCetak()
	{
		$nomor = $_REQUEST['nomor'];
		$sql = " SELECT a.*,b.name FROM TRX_RECEIPT a left join mst_coa b on
		a.coa_credit_id = b.id	WHERE NO_RECEIPT = '".$nomor."' "; 
		
		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryAll();
		$model = TrxReceipt::model()->find("trim(no_receipt)='".$nomor."'");
		
		$session=new CHttpSession;
            $session->open();
		Yii::import('application.extensions.giiplus.bootstrap.*');
		require_once('tcpdf/tcpdf.php');
		require_once('tcpdf/config/lang/eng.php');
              
		
		$html = $this->renderPartial('excelReceipt', array(
			'datas'=>$datas,
			'model'=>$model
		), true);
		
		//die($html);
		
			$pdf = new TCPDF();
			$pdf->SetCreator(PDF_CREATOR);
			$pdf->SetAutoPageBreak(TRUE, 0);
			$pdf->setPrintHeader(false);
			$pdf->setPrintFooter(false);
			$pdf->SetAutoPageBreak(TRUE, 0);
			$pdf->SetFont('dejavusans', '', 10);
			$pdf->AddPage();
			$pdf->writeHTML($html, true, true, true, true, '');
			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
			$pdf->LastPage();
		$pdf->Output("trx_receipt.pdf", "I");
		
	
	}
	
	public function actionShowTagihan()
	{
		/*$sql = " SELECT * FROM (
SELECT A.ID,'HG' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NOMINAL * C.NOMINAL AS FINAL_NOMINAL
FROM NDK_HEALTH A 
INNER JOIN REG_HEALTH_POLICY B ON A.REG_HEALTH_POLICY_ID = B.ID
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
UNION
SELECT A.ID,'NH' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NOMINAL * C.NOMINAL AS FINAL_NOMINAL
FROM NDK_HEALTH_NON A 
INNER JOIN REG_HEALTH_POLICY_NON B ON  A.REG_NONHEALTH_POLICY_ID = B.ID
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
UNION
SELECT A.ID,'VC' AS JNS_INSURANCE, A.NOTA_NOTE AS NOTA_DEBET_NO,A.NOMINAL
,CASE WHEN A.JNS_MATA_UANG = '1' THEN 'Rp. '
	WHEN A.JNS_MATA_UANG = '2' THEN '$'
	ELSE 'Euro'
END AS CODE , A.NOMINAL AS NOMINAL_CURR , A.NOMINAL AS NOMINAL_FINAL FROM TRX_VOUCHER A
)X
WHERE NOTA_DEBET_NO = '".$_POST['no_nota']."' "; */
		$sql = " SELECT * FROM (
SELECT A.ID,'HG' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NOMINAL * C.NOMINAL AS FINAL_NOMINAL,'24' as ID_INSURANCE
FROM NDK_HEALTH A 
INNER JOIN REG_HEALTH_POLICY B ON A.REG_HEALTH_POLICY_ID = B.ID
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
UNION
SELECT A.ID,'NH' AS JNS_INSURANCE,NOTA_DEBET_NO
,a.premium + a.tpl + a.adjusment + a.policy_cost + a.stampduty_asuransi + a.admin_broker + a.stamp_duty -a.discount - a.diskon_asuransi  as nominal
,D.CODE
,C.NOMINAL AS NOMINAL_CURR
,(a.premium + a.tpl + a.adjusment + a.policy_cost + a.stampduty_asuransi + a.admin_broker + a.stamp_duty -a.discount - a.diskon_asuransi) * C.NOMINAL AS FINAL_NOMINAL,
no_type as ID_INSURANCE
FROM NDK_HEALTH_NON A 
INNER JOIN REG_HEALTH_POLICY_NON B ON  A.REG_NONHEALTH_POLICY_ID = B.ID
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
LEFT JOIN MST_JNS_ASURANSI E ON B.mst_jns_asuransi_id = E.id
UNION
SELECT A.ID,'NP' AS JNS_INSURANCE,NO_INVOICE as NOTA_DEBET_NO,A.NOMINAL,'' CODE,A.NOMINAL AS NOMINAL_CURR,A.NOMINAL  AS FINAL_NOMINAL,to_char(mst_coa_id_kredit,'FM9999999999') as ID_INSURANCE
FROM NDK_PREMI_NON A 
UNION
SELECT a.id,'GANTIKARTU' as JNS_INSURANCE, a.NOTA_NO as NOTA_DEBET_NO, a.NOMINAL, 'Rp' as CODE, 1 as NOMINAL_CURR,a.NOMINAL * 1 as FINAL_NOMINAL,'1111' as ID_INSURANCE FROM ganti_kartu a
)X WHERE NOTA_DEBET_NO = '".$_POST['no_nota']."' ";

		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryRow();
		if(count($datas)>1){
			echo json_encode($datas);
		}else{
			echo json_encode(array("id"=>""));
		}
			Yii::app()->end();
	}
	
	function actionShowListnota(){
		$sql = " select x.* from (
select A.ID,'Premi Health' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NOMINAL * C.NOMINAL AS FINAL_NOMINAL,'24' as ID_INSURANCE,b.mst_client_id
from ndk_health a
inner join reg_health_policy b on a.reg_health_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
union
select A.ID,'P Non Health' AS JNS_INSURANCE,NOTA_DEBET_NO
,a.premium + a.tpl + a.adjusment + a.policy_cost + a.stampduty_asuransi + a.admin_broker + a.stamp_duty -a.discount - a.diskon_asuransi  as nominal
,D.CODE,C.NOMINAL AS NOMINAL_CURR
,(a.premium + a.tpl + a.adjusment + a.policy_cost + a.stampduty_asuransi + a.admin_broker + a.stamp_duty -a.discount - a.diskon_asuransi) * C.NOMINAL AS FINAL_NOMINAL,no_type as ID_INSURANCE,b.mst_client_id
from ndk_health_non a
inner join reg_health_policy_non b on a.reg_nonhealth_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
LEFT JOIN MST_JNS_ASURANSI E ON B.mst_jns_asuransi_id = E.id
union
SELECT a.id,'GK Health' as JNS_INSURANCE, a.NOTA_NO as NOTA_DEBET_NO, a.NOMINAL,'Rp' as CODE,  1 as NOMINAL_CURR,a.NOMINAL * 1 as FINAL_NOMINAL,'1111' as ID_INSURANCE,b.mst_client_id FROM ganti_kartu a 
inner join reg_health_policy b on a.reg_policy_id = b.id
where a.type_reg_policy = '1'
union
SELECT a.id,'GK Health' as JNS_INSURANCE, a.NOTA_NO as NOTA_DEBET_NO,
 a.NOMINAL,'Rp' as CODE,  1 as NOMINAL_CURR,a.NOMINAL * 1 as FINAL_NOMINAL,'1111' as ID_INSURANCE,
 b.mst_client_id FROM ganti_kartu a 
inner join reg_health_policy_non b on a.reg_policy_id = b.id
where a.type_reg_policy = '2'
union
select A.ID,'NK Health' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NOMINAL * C.NOMINAL AS FINAL_NOMINAL,'24' as ID_INSURANCE,b.mst_client_id
from ndk_health_kredit a
inner join reg_health_policy b on a.reg_health_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
union
select A.ID,'NK Non Health' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NOMINAL * C.NOMINAL AS FINAL_NOMINAL,no_type as ID_INSURANCE,b.mst_client_id
from ndk_health_non_kredit a
inner join reg_health_policy_non b on a.reg_nonhealth_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
LEFT JOIN MST_JNS_ASURANSI E ON B.mst_jns_asuransi_id = E.id
)x left join trx_receipt y on x.nota_debet_no = y.nota_note WHERE y.nota_note is null AND x.mst_client_id = '".$_POST['id_client']."' ";

if($_POST['jns_trx']=="1"){
	$sql .=" AND NOTA_DEBET_NO like 'HG%' ";
}

if($_POST['jns_trx']=="2"){
	$sql .=" AND NOTA_DEBET_NO like 'NP%' ";
}
$sql .= " ORDER BY NOTA_DEBET_NO ";


		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryAll();
		if(count($datas)>=1){
			echo json_encode($datas);
		}else{
			echo json_encode(array("id"=>""));
		}
			Yii::app()->end();
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new TrxReceipt;
		$model2=new MstClient('searchAllclient');
		$model2->unsetAttributes();  
		$model3=new MstClient('searchAllnota');
		$model3->unsetAttributes();  
		if (isset($_GET['MstClient'])) {
			$model2->attributes=$_GET['MstClient'];
			$model3->attributes=$_GET['MstClient'];
		}
		
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['TrxReceipt'])) {
			$model->attributes=$_POST['TrxReceipt'];
			$nota_note = $_POST['nota_note'];
			$client_name = $_POST['client_name'];
			$mst_client_id = $_POST['mst_client_id'];
			
			$coa_debet_id = $_POST['coa_debet_id'];
			$tgl_transaksi = $_POST['tgl_transaksi'];
			$jns_trx = $_POST['jns_trx'];
			$coa_credit_id = $_POST['coa_credit_id'];
			$jns_mata_uang = $_POST['jns_mata_uang'];
			$nominal = $_POST['nominal'];
			$keterangan = $_POST['keterangan'];
			$nomor = "";
			for($i=0;$i<count($nota_note);$i++)
			{
				if($jns_trx[$i]!="0"){
					/*if($jns_trx[$i]){//jika trx premi
						$sql = " UPDATE NDK_HEALTH SET IS_BAYAR = '1' AND JML_BAYAR = '".$nominal[$i]."' WHERE NOTA_DEBET_NO = '".$nota_note[$i]."' ";
						Yii::app()->db->createCommand($sql)->queryAll();
						
						$sql2 = " UPDATE NDK_HEALTH_NON SET IS_BAYAR = '1' AND JML_BAYAR = '".$nominal[$i]."'  WHERE NOTA_DEBET_NO = '".$nota_note[$i]."' ";
						Yii::app()->db->createCommand($sql2)->queryAll();
					}else{ */
						$model=new TrxReceipt;
						if($i != 0){
							$model->no_receipt = $nomor;
						}
						$model->client_name = $_POST['TrxReceipt']['client_name'];
						$model->mst_client_id = $_POST['TrxReceipt']['mst_client_id'];
						$model->coa_debet_id = $_POST['TrxReceipt']['coa_debet_id'];
						$model->tgl_transaksi = date('Y-m-d',strtotime($_POST['TrxReceipt']['tgl_transaksi']));
						$model->jns_trx = $jns_trx[$i];
						$model->nota_note = $nota_note[$i];
						$model->coa_credit_id = $coa_credit_id[$i];
						$model->jns_mata_uang = $jns_mata_uang[$i];
						$model->nominal = trim($nominal[$i]);
						$model->keterangan = $keterangan[$i];
						$model->save();
						if($i==0){
							$nomor = "RC-".date("Ymd").$model->id;
							TrxReceipt::model()->updateByPk($model->id,array('no_receipt'=>$nomor));
						}
					//}
				}
			}
				$this->redirect(array('admin'));
		}
		
		
		$sql = " 
			select case when a.code like '%00' then a.name
			else '--'||a.name 
			end as name,a.id,a.code
			from mst_coa a
			order by a.code
		";
		$cmd = Yii::app()->db->createCommand($sql);
		$data = $cmd->queryAll();

		$this->render('create',array(
			'model'=>$model,
			'model2'=>$model2,
			'model3'=>$model3,
			'data'=>$data,
		));
	}
	
	public function actionPosting()
	{	
		$no_receipt = $_REQUEST['no_receipt'];
		$sql = " UPDATE trx_receipt SET posting = 2 WHERE no_receipt = '".$no_receipt."' ";
		Yii::app()->db->createCommand($sql)->queryAll();

		$model=new TrxReceipt('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['TrxReceipt'])) {
			$model->attributes=$_GET['TrxReceipt'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
		
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($no_receipt)
	{
		$model=$this->loadRecipt($no_receipt);
		$model2=new MstClient('searchAllclient');
		$model2->unsetAttributes();  
		$model3=new MstClient('searchAllnota');
		$model3->unsetAttributes();  
		if (isset($_GET['MstClient'])) {
			$model2->attributes=$_GET['MstClient'];
			$model3->attributes=$_GET['MstClient'];
		}
		
	
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['TrxReceipt'])) {
			$model->attributes=$_POST['TrxReceipt'];
			$id = $_POST['idnya'];
			$nota_note = $_POST['nota_note'];
			$client_name = $_POST['client_name'];
			$mst_client_id = $_POST['mst_client_id'];
			
			$coa_debet_id = $_POST['coa_debet_id'];
			$tgl_transaksi = $_POST['tgl_transaksi'];
			$jns_trx = $_POST['jns_trx'];
			$coa_credit_id = $_POST['coa_credit_id'];
			$jns_mata_uang = $_POST['jns_mata_uang'];
			$nominal = $_POST['nominal'];
			$keterangan = $_POST['keterangan'];
			$nomor = "";
			for($i=0;$i<count($nota_note);$i++)
			{
				if($jns_trx[$i]!="0"){
					/*if($jns_trx[$i]){//jika trx premi
						$sql = " UPDATE NDK_HEALTH SET IS_BAYAR = '1' AND JML_BAYAR = '".$nominal[$i]."' WHERE NOTA_DEBET_NO = '".$nota_note[$i]."' ";
						Yii::app()->db->createCommand($sql)->queryAll();
						
						$sql2 = " UPDATE NDK_HEALTH_NON SET IS_BAYAR = '1' AND JML_BAYAR = '".$nominal[$i]."'  WHERE NOTA_DEBET_NO = '".$nota_note[$i]."' ";
						Yii::app()->db->createCommand($sql2)->queryAll();
					}else{ */
						if(empty($id[$i])){
						$model=new TrxReceipt;
						}else{
						$model=TrxReceipt::model()->findByPk((int)$id[$i]);
						}
						if($i != 0){
							$model->no_receipt = $nomor;
						}
						$model->client_name = $_POST['TrxReceipt']['client_name'];
						$model->mst_client_id = $_POST['TrxReceipt']['mst_client_id'];
						$model->coa_debet_id = $_POST['TrxReceipt']['coa_debet_id'];
						$model->tgl_transaksi = date('Y-m-d',strtotime($_POST['TrxReceipt']['tgl_transaksi']));
						$model->jns_trx = $jns_trx[$i];
						$model->nota_note = $nota_note[$i];
						$model->coa_credit_id = $coa_credit_id[$i];
						$model->jns_mata_uang = $jns_mata_uang[$i];
						$model->nominal = trim($nominal[$i]);
						$model->keterangan = $keterangan[$i];
						$model->save();
						if($i==0){
							$nomor = "RC-".date("Ymd").$model->id;
							TrxReceipt::model()->updateByPk($model->id,array('no_receipt'=>$nomor));
						}
					//}
				}
			}
				$this->redirect(array('admin'));
		}

		$this->render('update',array(
			'model'=>$model,
			'model2'=>$model2,
			'model3'=>$model3,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if (!isset($_GET['ajax'])) {
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
			}
		} else {
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('TrxReceipt');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new TrxReceipt('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['TrxReceipt'])) {
			$model->attributes=$_GET['TrxReceipt'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return TrxReceipt the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=TrxReceipt::model()->findByPk($id);
		if ($model===null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		return $model;
	}
	
	public function loadRecipt($no_receipt)
	{
		$model=TrxReceipt::model()->find('no_receipt=\''.$no_receipt.'\'');
		if ($model===null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param TrxReceipt $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax']==='trx-receipt-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}